package com.loongtech.bi.action.bigame;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.game.LevelGameManager;
import com.loongtech.bi.manager.game.LevelGameManager.ActiveUser10LevelDistributeData;
import com.loongtech.bi.manager.game.LevelGameManager.ActiveUser1LevelDistributeData;
import com.loongtech.bi.manager.game.LevelGameManager.NewUser1LevelDistributeData;
import com.loongtech.bi.manager.game.LevelGameManager.UserSevenLoss1LevelDistributeData;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游戏内分析-等级分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/game/levelUserGame")
public class LevelUserGameBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
    private Date dayDate = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 前端传过来的开始时间

    private List<ActiveUser1LevelDistributeData> dataList1;  // 前端要显示的数据1
    private ChartUIData chartData1;  // 前端要显示的柱状图1数据

    private List<ActiveUser10LevelDistributeData> dataList2;  // 前端要显示的数据1
    private ChartUIData chartData2;  // 前端要显示的柱状图1数据

    private List<NewUser1LevelDistributeData> dataList3;  // 前端要显示的数据1
    private ChartUIData chartData3;  // 前端要显示的柱状图1数据

    private List<UserSevenLoss1LevelDistributeData> dataList4;  // 前端要显示的数据1
    private ChartUIData chartData4;  // 前端要显示的柱状图1数据

    private List<LevelGameManager.NewUser1LevelDistributeData1> dataList100;
    private ChartUIData chartData100;

    @Autowired
    private LevelGameManager levelGameManager;

    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dsearch, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dayDate = Time.formatDate(dsearch, Time.TIMEPATTERN_SIMPLE);
            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            dataList1 = levelGameManager.getActiveUser1LevelDistributeData(dayDate, queryPlatform, queryChannel, queryServer);
            if (dataList1 != null && dataList1.size() > 0) {
                chartData1 = new ChartUIData();
                chartData1.setJsonType(ChartType.SPLINE.type);
                chartData1.setJsonStr(new ChartTool(dataList1, ActiveUser1LevelDistributeData.class, "").loadData(chartData1.getJsonType(), true));
            } else {
                chartData1 = null;
            }

            dataList2 = levelGameManager.getActiveUser10LevelDistributeData(dayDate,queryPlatform, queryChannel, queryServer);
            if (dataList2 != null && dataList2.size() > 0) {
                chartData2 = new ChartUIData();
                chartData2.setJsonType(ChartType.COLUMN.type);
                chartData2.setJsonStr(new ChartTool(dataList2, ActiveUser10LevelDistributeData.class, "").loadData(chartData2.getJsonType(), true));
            } else {
                chartData2= null;
            }

            dataList100 = levelGameManager.getNewUser1LevelDistributeData1(dayDate, queryPlatform, queryChannel, queryServer);
            if (dataList100 != null && dataList100.size() > 0) {
                chartData100 = new ChartUIData();
                chartData100.setJsonType(ChartType.SPLINE.type);
                chartData100.setJsonStr(new ChartTool(dataList100, LevelGameManager.NewUser1LevelDistributeData1.class, "").loadData(chartData100.getJsonType(), true));
            } else {
                chartData100 = null;
            }

            result.put("res", "0");
            result.put("dataList1", dataList1);
            result.put("chartData1", chartData1);
            result.put("dataList2", dataList2);
            result.put("chartData2", chartData2);
            result.put("dataList100", dataList100);
            result.put("chartData100", chartData100);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "search2.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search2(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            dataList3 = levelGameManager.getNewUser1LevelDistributeData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
            if (dataList3 != null && dataList3.size() > 0) {
                chartData3 = new ChartUIData();
                chartData3.setJsonType(ChartType.SPLINE.type);
                chartData3.setJsonStr(new ChartTool(dataList3, NewUser1LevelDistributeData.class, "").loadData(chartData3.getJsonType(), true));
            } else {
                chartData3 = null;
            }

            dataList4 = levelGameManager.getUserSecondLoss1LevelDistributeData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
            if (dataList4 != null && dataList4.size() > 0) {
                chartData4 = new ChartUIData();
                chartData4.setJsonType(ChartType.SPLINE.type);
                chartData4.setJsonStr(new ChartTool(dataList4, UserSevenLoss1LevelDistributeData.class, "").loadData(chartData4.getJsonType(), true));
            } else {
                chartData4 = null;
            }

            result.put("res", "0");
            result.put("dataList3", dataList3);
            result.put("chartData3", chartData3);
            result.put("dataList4", dataList4);
            result.put("chartData4", chartData4);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "updateServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer = server;
            selectedServer();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        try {
            queryChannel = channel;
            updateServerItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform = platform;
            updateChannelItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @Override
    public void search() {
    }

    @Override
    protected String csvContent() {
        return "等级分布";
    }

    @Override
    protected String csvFileName() {
        return "等级分布";
    }

    public Date getDayDate() {
        return dayDate;
    }

    public void setDayDate(Date dayDate) {
        this.dayDate = dayDate;
    }

    public List<ActiveUser1LevelDistributeData> getDataList1() {
        return dataList1;
    }

    public void setDataList1(List<ActiveUser1LevelDistributeData> dataList1) {
        this.dataList1 = dataList1;
    }

    public ChartUIData getChartData1() {
        return chartData1;
    }

    public void setChartData1(ChartUIData chartData1) {
        this.chartData1 = chartData1;
    }

    public List<ActiveUser10LevelDistributeData> getDataList2() {
        return dataList2;
    }

    public void setDataList2(List<ActiveUser10LevelDistributeData> dataList2) {
        this.dataList2 = dataList2;
    }

    public ChartUIData getChartData2() {
        return chartData2;
    }

    public void setChartData2(ChartUIData chartData2) {
        this.chartData2 = chartData2;
    }

    public List<NewUser1LevelDistributeData> getDataList3() {
        return dataList3;
    }

    public void setDataList3(List<NewUser1LevelDistributeData> dataList3) {
        this.dataList3 = dataList3;
    }

    public ChartUIData getChartData3() {
        return chartData3;
    }

    public void setChartData3(ChartUIData chartData3) {
        this.chartData3 = chartData3;
    }

    public List<UserSevenLoss1LevelDistributeData> getDataList4() {
        return dataList4;
    }

    public void setDataList4(List<UserSevenLoss1LevelDistributeData> dataList4) {
        this.dataList4 = dataList4;
    }

    public ChartUIData getChartData4() {
        return chartData4;
    }

    public void setChartData4(ChartUIData chartData4) {
        this.chartData4 = chartData4;
    }

    public List<LevelGameManager.NewUser1LevelDistributeData1> getDataList100() {
        return dataList100;
    }

    public void setDataList100(List<LevelGameManager.NewUser1LevelDistributeData1> dataList100) {
        this.dataList100 = dataList100;
    }

    public ChartUIData getChartData100() {
        return chartData100;
    }

    public void setChartData100(ChartUIData chartData100) {
        this.chartData100 = chartData100;
    }
}
